package com.mifengs.mall.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.mall.entity.ResponseBean;
import com.mifengs.mall.entity.TokenBean;
import com.mifengs.mall.netWork.NetWork;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class VerifyPhoneActivity extends BaseActivity {

    @BindView(R.id.et_iphone)
    EditText mEtIphone;
    @BindView(R.id.iv_delete)
    ImageView mIvDelete;
    private String mWeixinJson;
    private String mMoblie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_phone);
        ButterKnife.bind(this);
        setStatusColorAndMode(R.color.white, true);
        initNavigationBar(true, "绑定手机号");
        init();
    }

    private void init() {
        mWeixinJson = getIntent().getStringExtra("weixinJson");
        mEtIphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    mIvDelete.setVisibility(View.INVISIBLE);
                }else{
                    mIvDelete.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    @OnClick({R.id.iv_delete, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_delete:
                mEtIphone.setText("");
                mIvDelete.setVisibility(View.GONE);
                break;
            case R.id.next:
                mMoblie = mEtIphone.getText().toString();
                if (TextUtils.isEmpty(mMoblie)) {
                    showLongToast("请输入手机号");
                } else {
                    verifyPhone(mMoblie);
                }
                break;
        }
    }

    public void verifyPhone(String mobile) {
        showLoading();
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("isWeixin", "0");
        NetWork.getLoginApi().phoneIsExitsa(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        hideLoading();
                    }

                    @Override
                    public void onNext(ResponseBean tokenBeanResponseBean) {
                        hideLoading();
                        String status = tokenBeanResponseBean.getStatus();
                        if (status.equals(Constant.SUCCESS_CODE)) {
                            //sendSMSCode(mMoblie,1);
                            toCompletePwd(1);
                        }else if("1015".equals(status)){
                            //sendSMSCode(mMoblie,0);
                            toCompletePwd(0);
                        } else if("1016".equals(status)){
                            //sendSMSCode(mMoblie,0);
                            toCompletePwd(0);
                        } else {
                            showLongToast(tokenBeanResponseBean.getInfo());
                        }
                    }
                });
    }


    public void toCompletePwd(int isRegType) {
        Intent intent = new Intent(this, CompletePasswordActivity.class);
        intent.putExtra("weixinJson", mWeixinJson);
        intent.putExtra("mobile", mMoblie);
        intent.putExtra("isRegType", isRegType);
        startActivity(intent);
        finish();
    }

    public void sendSMSCode(String mMoblie,final int isRegType) {
        showLoading();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mobile", mMoblie);
        hashMap.put("isBindType","0");
        NetWork.getLoginApi().sendSMSCodeRegister(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<ResponseBean<TokenBean>>bindToLife())
                .subscribe(new Observer<ResponseBean<TokenBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        showLongToast(throwable.toString());
                        hideLoading();
                    }

                    @Override
                    public void onNext(ResponseBean<TokenBean> responseBean) {
                        hideLoading();
                        String status = responseBean.getStatus();
                        if (status.equals(Constant.SUCCESS_CODE)) {
                            toCompletePwd(isRegType);
                        } else {
                            showLongToast(responseBean.getInfo());
                        }
                    }
                });
    }


}
